﻿using BLL;
using Ozone.BLL;
using Ozone.Models;
using Ozone.Views.MethodCenter;
using Ozone.Views.Widget;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ozone.Views.UserCenter
{
    /// <summary>
    /// UCUserCenterMain.xaml 的交互逻辑
    /// </summary>
    public partial class UCUserCenterMain : UserControl
    {
        private List<sys_user> sys_UserList;

        public UCUserCenterMain()
        {
            InitializeComponent();
            ReFreshDgUserList();  //刷新用户列表
        }

        private void buttonMethodItem_click(object sender, RoutedEventArgs e)
        {
            string tag = ((Button)sender).Tag.ToString();
            if (dgUserList.SelectedItems != null)
            {
                sys_user sys_UserModel = (sys_user)dgUserList.SelectedItem;
                //添加
                if (tag == "ADD")
                {
                    WindowUserInfo userInfoWindow = new WindowUserInfo(sys_UserModel);
                    if (userInfoWindow.ShowDialog() == false)
                    {
                        return;
                    }
                    sys_UserModel = userInfoWindow.sys_UserModel;
                    Sys_UserBLL.AddUserInfo(sys_UserModel);
                }
                //修改
                if (tag == "UPDATE")
                {
                    WindowUserInfo userInfoWindow = new WindowUserInfo(sys_UserModel);
                    if (userInfoWindow.ShowDialog() == false)
                    {
                        return;
                    }
                    sys_UserModel = userInfoWindow.sys_UserModel;
                    Sys_UserBLL.UpdateUserInfo(sys_UserModel);
                }
                //删除
                if (tag == "DEL")
                {
                    if (sys_UserModel != null)
                    {
                        WindowConfirmationDialog confirmationDialog = new WindowConfirmationDialog("确认删除此用户吗？");
                        if (confirmationDialog.ShowDialog() == false)
                        {
                            return;
                        }
                        sys_UserModel.Status = 0;
                        Sys_UserBLL.UpdateUserInfo(sys_UserModel);
                    }
                }
            }
            ReFreshDgUserList();
        }

        /// <summary>
        /// 刷新用户列表
        /// </summary>
        private void ReFreshDgUserList()
        {
            sys_UserList = Sys_UserBLL.GetUserInfoList(null, null);
            if (sys_UserList != null)
            {
                dgUserList.ItemsSource = null;
                dgUserList.ItemsSource = sys_UserList;
            }
        }
    }
}